using System;
using System.Web;
using System.Web.Mvc;
using Elmah;
using PicStore.Common.Ioc;
using PicStore.Common.Log;

namespace PicStore.Web.Filters
{
    public class HandleExceptionAttribute:HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {

            this.LogException(filterContext.Exception);
            //filterContext.Controller.ViewData["Error"] = filterContext.Exception.Message;
            //ViewDataDictionary viewData = filterContext.Controller.ViewData;


            filterContext.Result = new ViewResult
            {
                ViewName = View,
            };
            //// filterContext.HttpContext.Response.Clear();

            filterContext.ExceptionHandled = true;
            base.OnException(filterContext);
        } 


        private void LogException(Exception e)
        {
            var log = ObjectFactory.GetInstance<ILog>();
            log.Severe(e.ToString());

            ElmahLog(e);
        }

        private void ElmahLog(Exception exception)
        {
            var context = HttpContext.Current;
            ErrorLog.GetDefault(context).Log(new Elmah.Error(exception, context));
        }
    }
}